#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  // 加载登录页面
  _login_dlg = new LoginDialog(this);
  setCentralWidget(_login_dlg);
  // _login_dlg->show();

  // 创建和注册消息连接
  connect(_login_dlg, &LoginDialog::switchRegister, this,
          &MainWindow::SlotSwitchRegister);

  _register_dlg = new RegisterDialog(this);

  // dialog嵌入到mainwindow
  _login_dlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
  _register_dlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
  _register_dlg->hide();
}

MainWindow::~MainWindow() {
  delete ui;
  // _login_dlg 新建对象没有挂载到父节点
  // if (_login_dlg) {
  //   delete _login_dlg;
  //   _login_dlg = nullptr;
  // }
  // if (_register_dlg) {
  //   delete _register_dlg;
  //   _register_dlg = nullptr;
  // }
}

void MainWindow::SlotSwitchRegister() {
  // 切换注册对话框
  setCentralWidget(_register_dlg);
  // 隐藏登录对话框
  _login_dlg->hide();
  _register_dlg->show();
}
